package com.hyf.rabbitmq.remoteRpc;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RPCServer {


    public static final String RPC_QUEUE_NAME="rpc_queue";

    //计算斐波切数，测试用
    private static int fib(int n){
        if(n==0)
            return 0;
        if(n==1)
            return 1;
        return fib(n-1)+fib(n-2);
    }

    public static void main(String[] args){

        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("admin");
        factory.setPassword("12345");
        factory.setVirtualHost("/");
        factory.setPort(5672);
        //第二种连接方式
        //factory.setUri("amqp://userName:password@hostName:portNumber/virtualHost");
        Connection connection =null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare(RPC_QUEUE_NAME,false,false,false,null);
            //代表每次接受一个任务
            channel.basicQos(1);

            QueueingConsumer consumer=new QueueingConsumer(channel);
            channel.basicConsume(RPC_QUEUE_NAME,false,consumer);
            System.out.println("RPCServer [x] awaiting RPC requests");
            while(true){
                String response=null;
                QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                AMQP.BasicProperties properties = delivery.getProperties();
                AMQP.BasicProperties replyProps=new AMQP.BasicProperties.Builder().correlationId(properties.getCorrelationId()).build();
                try {

                    String message=new String(delivery.getBody(),"utf-8");
                    //测试计算斐波那契数
                    int n=Integer.parseInt(message);
                    System.out.println("RPCServer [.] fib("+message+")");
                    response=""+fib(n);
                }catch (Exception e){
                    e.printStackTrace();
                    response="";
                }finally{
                    channel.basicPublish("",properties.getReplyTo(),replyProps,response.getBytes("utf-8"));
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally{
            /*if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }*/
        }


    }
}
